package leetcode.p1418;

import java.util.*;

/**
 * @author: trtan
 * @date: 2021-07-06 19:27
 **/
public class DisplayTableRestaurant {
    /**
     * Map套娃Map 记录每个餐桌每种菜品出现了多少次
     * @param orders
     * @return java.util.List<java.util.List < java.lang.String>>
     * @author trtan
     * @date 2021/7/6 19:45
     */
    public List<List<String>> displayTable(List<List<String>> orders) {
        Map<Integer, Map<String, Integer>> map = new HashMap<>();
        // 菜品
        TreeSet<String> dished = new TreeSet<>();
        // 桌子编号
        TreeSet<Integer> tables = new TreeSet<>();
        for (int i = 0; i < orders.size(); i++) {
            List<String> list = orders.get(i);
            // 桌子编号
            Integer tableNo = Integer.valueOf(list.get(1));
            tables.add(tableNo);
            // 菜品
            String dishes = list.get(2);
            dished.add(dishes);
            Map<String, Integer> table = map.getOrDefault(tableNo, new HashMap<>());
            Integer value = table.getOrDefault(dishes, 0);
            // 更新当前桌子当前菜品的出现次数
            table.put(dishes, value + 1);
            map.put(tableNo, table);
        }
        List<List<String>> result = new ArrayList<>();
        // 首先第一行
        ArrayList<String> one = new ArrayList<>();
        one.add("Table");
        one.addAll(dished);
        result.add(one);
        for (Integer tableNo : tables) {
            ArrayList<String> line = new ArrayList<>();
            line.add(tableNo + "");
            Map<String, Integer> table = map.get(tableNo);
            for (String dishes : dished) {
                int value = table.getOrDefault(dishes, 0);
                line.add(value + "");
            }
            result.add(line);
        }
        return result;
    }
}
